package com.genesis.yunnanji.view;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.bean.AddressBean;
import com.genesis.yunnanji.bean.MessageEventBean;
import com.genesis.yunnanji.widget.PickerViewUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

import static com.genesis.yunnanji.view.DeliveryAddressActivity.mAddressBean;

/**
 * 创建日期：2018/9/25 on 9:09
 * 作者:Kite
 * 描述:新增收货地址
 */
public class AddDeliveryAddressActivity extends BaseActivity {

    @BindView(R.id.et_name)
    EditText etName;

    @BindView(R.id.et_phone)
    EditText etPhone;

    @BindView(R.id.et_address)
    EditText etAddress;

    @BindView(R.id.iv_choose)
    ImageView ivChoose;

    @BindView(R.id.tv_choose_area)
    TextView tvChooseArea;

    @BindView(R.id.iv_unchoose)
    ImageView ivUnchoose;

    private boolean isClick = true;

    private int mIsDefault = 1;//0不默认，1默认

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_delivery_address;
    }

    @Override
    public void initView() {
        super.initView();
        setTitle("新增收货地址");
        addMenuTextItem(R.string.save, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mAddressBean.add(new AddressBean(etName.getText().toString(), etPhone.getText().toString(), etAddress.getText().toString(), mIsDefault));
                EventBus.getDefault().post(new MessageEventBean(7, tvChooseArea.getText().toString()));
                finish();
            }
        });
        Bundle bundle = null;
        if (bundle == null) {
            int p0 = getIntent().getIntExtra("p0", 0);
            if (p0 == 1) {
                AddressBean p1 = (AddressBean) getIntent().getSerializableExtra("p1");
                etName.setText(p1.getName());
                etName.setSelection(etName.getText().length());
                etPhone.setText(p1.getMobile());
                etPhone.setSelection(etPhone.getText().length());
                etAddress.setText(p1.getAddress());
                etAddress.setSelection(etAddress.getText().length());
            }
        }
    }

    @OnClick({R.id.tv_choose_area, R.id.ll_choose})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_choose_area:
                PickerViewUtil.getInstance().showCityPicker(this);
                PickerViewUtil.getInstance().setOnCityCompleteListener(new PickerViewUtil.OnCityCompleteListener() {
                    @Override
                    public void onSelectCity(String province, String city, String area) {
                        tvChooseArea.setText(province + city);
                    }
                });
                break;
            case R.id.ll_choose:
                if (isClick) {
                    ivUnchoose.setVisibility(View.VISIBLE);
                    ivChoose.setVisibility(View.GONE);
                    mIsDefault = 0;
                    isClick = false;
                } else {
                    ivUnchoose.setVisibility(View.GONE);
                    ivChoose.setVisibility(View.VISIBLE);
                    mIsDefault = 1;
                    isClick = true;
                }
                break;
        }
    }
}
